package com.egg.ylt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.adapter.ADA_SwitchBaby;
import com.egg.ylt.pojo.SwitchBabyEntity;
import com.egg.ylt.presenter.SwitchBabyPresenter;
import com.egg.ylt.view.ISwitchBabyView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.eventbus.EventCenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lijiateng on 2019/1/21.
 * <p>
 * 通过 startActivityForResult 调用此类
 * 选中的 babyId 通过 intent 回传，key 值为 check_baby
 */

public class ACT_SwitchBaby extends BaseActivity<SwitchBabyPresenter> implements ISwitchBabyView {

    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_tv_right)
    TextView includeTvRight;
    @BindView(R.id.include_iv_right)
    ImageView includeIvRight;
    @BindView(R.id.rl_include_title)
    RelativeLayout rlIncludeTitle;
    @BindView(R.id.rv_baby)
    XRecyclerView rvBaby;

    private ADA_SwitchBaby mAdapter;
    private AppSharedPreferences mSp;

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_switch_baby;
    }

    @Override
    protected View getLoadingTargetView() {
        return rvBaby;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_style_orange);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, rlIncludeTitle);

        includeTvTitle.setText("宝宝管理");
        includeTvRight.setVisibility(View.GONE);
        includeIvRight.setVisibility(View.VISIBLE);
        includeIvRight.setImageResource(R.mipmap.baby_icon_add);

        rvBaby.setPullRefreshEnabled(false);
        rvBaby.setLoadingMoreEnabled(false);

        mSp = new AppSharedPreferences(mContext);

        mAdapter = new ADA_SwitchBaby(mContext);
        rvBaby.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvBaby.setAdapter(mAdapter);

        mPresenter.requestBabyList();

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.SWITCH_BABY_EVENT:

                // 保存在本地
                String checkedId = (String) eventCenter.getData();
                mSp.putString(Constants.BABY_ID, checkedId);
                Constants.BABYID = checkedId;

                // 回传到上个页面
                Intent intent = new Intent();
                intent.putExtra(Constants.BABY_ID, checkedId);
                setResult(RESULT_OK, intent);
                finish();

                break;
        }
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return true;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return TransitionMode.RIGHT;
    }

    @OnClick({R.id.include_iv_back, R.id.include_iv_right, R.id.rv_baby})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back:
                finish();
                break;
            case R.id.include_iv_right:
                ACT_SelectBaby.startActResource(mContext, ACT_EditBabyInfo.START_FROM_SWITCH);
                break;

        }
    }

    @Override
    public void updateBabyList(List<SwitchBabyEntity.ListBean> list) {
        String checkBaby = mSp.getString(Constants.BABY_ID, "");
        for (SwitchBabyEntity.ListBean listBean : list) {
            String babyId = listBean.getId();
            listBean.setChecked(checkBaby.equals(babyId));
        }

        mAdapter.update(list, true);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 添加宝宝后刷新列表
        mPresenter.requestBabyList();
    }
}
